"""
@This file will crate popular tags for the mentioned tables, this will be used to create 
feature  for classification 
"""

import MySQLdb
from collections import Counter

"""

database = "isec2014"
data_table = "qimp_migration_124_41_aug13"
user = "root"
password="123"
port=3306
file_path2 = "parent_id" # These are the ids for which I am using their parent values
"""

database = "sangeeta"
data_table = "qimp_migration_124_41_aug13"
user = "sangeetal"
password="sangeetal"
port=3307
file_path2 = "parent_id" # These are the ids for which I am using their parent values
#"""

#site = "stackoverflow"
site = "serverfault"
#site = "superuser"
#site = "dba.stackexchange"
#site = "programmers.stackexchange"

table = "posts_"+site+"_aug13"

db1= MySQLdb.connect(host="localhost", user=user, passwd=password,db=database, port=port)
select_cursor = db1.cursor()
insert_cursor = db1.cursor()

str1 = "select tags from  `"+ table+  "`  where posttypeid = 1 and accepted_answer_id!=-1"
select_cursor.execute(str1)
data = select_cursor.fetchall()

tags = list()
for t in data:
    temp = t[0]
    number_of_tags = len(temp.split(">"))-1
    temp_tags = temp.split(">")
    #temp_tags = tag_str.split(">")
    #tags = list()
    
    count = 1
    for t in temp_tags:
        if count<=number_of_tags:
            tags.append(t.split("<")[1])
            count = count+1
  

tag_with_count=Counter(tags)
#for t in sorted(tag_with_count.values()):
#    print t, t.get()

insert_table = "tags_count_"+site+"_aug13"
for t in tag_with_count:
    insert_str= "insert into `" +insert_table+ "` values('"+t+"',"+(str)(tag_with_count[t])+")"
    insert_cursor.execute(insert_str)
    print "insert=", insert_str

db1.commit()     

print tag_with_count